package com.qcby.llmtchstd.service.Impl;

import com.qcby.llmtchstd.common.ResponseResult;
import com.qcby.llmtchstd.dao.ExamDao;
import com.qcby.llmtchstd.entity.Exam;
import com.qcby.llmtchstd.entity.Note;
import com.qcby.llmtchstd.service.ExamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ExamServiceImpl implements ExamService {
    @Autowired
    private ExamDao examDao;
    @Override
    public int addExam(Exam exam) {
        return examDao.addExam(exam);
    }

    @Override
    public ResponseResult count() {
        try {
            Integer num = examDao.count();
            if (num != null) {
                return ResponseResult.success(num); // 查询成功，返回数量
            } else {
                return ResponseResult.fail("5001", "页码查询失败！"); // 处理结果为空的情况
            }
        }
        catch (Exception e) {
            e.printStackTrace();//打印异常到控制台
            return ResponseResult.dbError("数据库异常：查询考试数量失败！"); // 统一处理数据库异常
        }
    }

    @Override
    public ResponseResult page(Exam exam) {
        try {
            List<Exam> list=examDao.page(exam);
            return ResponseResult.success(list);
        }
        catch (Exception e){
            e.printStackTrace();//打印异常到控制台
            return ResponseResult.dbError("数据库异常：查询考试数量失败");
        }
    }

    @Override
    public ResponseResult search(Exam exam) {
        try {
            List<Exam> list=examDao.search(exam);
            if(list.size()!=0){
                return ResponseResult.success(list);
            }
            return ResponseResult.fail("4003","教案不存在，请检查数据");
        }
        catch (Exception e){
            e.printStackTrace();//打印异常到控制台
            return ResponseResult.dbError("数据库异常：查询考试数量失败");
        }
    }

    @Override
    public ResponseResult serCount(Exam exam) {
        try {
            Integer num = examDao.serCount(exam);
            if (num != null) {
                return ResponseResult.success(num); // 查询成功，返回数量
            } else {
                return ResponseResult.fail("5001", "页码查询失败！"); // 处理结果为空的情况
            }
        }
        catch (Exception e) {
            e.printStackTrace();//打印异常到控制台
            return ResponseResult.dbError("数据库异常：查询考试数量失败！"); // 统一处理数据库异常
        }
    }

    @Override
    public ResponseResult generate(Exam exam) {
        try{
            int num=examDao.generate(exam);
            if(num==1){
                return ResponseResult.success();
            }
            return ResponseResult.fail("4003","数据传输失败，请检查数据");
        }
        catch (Exception e){
            e.printStackTrace();//打印异常到控制台
            return ResponseResult.dbError("数据库异常：数据传输失败");
        }
    }

    @Override
    public ResponseResult generateAll(Exam exam) {
        try{
            int num=examDao.generateAll(exam);
            if(num==1){
                return ResponseResult.success();
            }
            return ResponseResult.fail("4003","数据传输失败，请检查数据");
        }
        catch (Exception e){
            e.printStackTrace();//打印异常到控制台
            return ResponseResult.dbError("数据库异常：数据传输失败");
        }
    }

    @Override
    public ResponseResult publish(Exam exam) {
        try{
            int num=examDao.publish(exam);
            int num0=examDao.reset(exam);
            if(num==1 ){

                return ResponseResult.success();
            }
            return ResponseResult.fail("4003","数据传输失败，请检查数据");
        }
        catch (Exception e){
            e.printStackTrace();//打印异常到控制台
            return ResponseResult.dbError("数据库异常：数据传输失败");
        }
    }

    @Override
    public ResponseResult generateAlll(Exam exam) {
        try{
            int num=examDao.generateAlll(exam);
            if(num==1){
                return ResponseResult.success();
            }
            return ResponseResult.fail("4003","数据传输失败，请检查数据");
        }
        catch (Exception e){
            e.printStackTrace();//打印异常到控制台
            return ResponseResult.dbError("数据库异常：数据传输失败");
        }
    }
}
